/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-17     armink       the first version
 */

#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_

#include <rtconfig.h>
#include <board.h>

/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev at32f437_onchip_flash;
extern struct fal_flash_dev nor_flash0;

/* flash device table */
#define FAL_FLASH_DEV_TABLE     \
    {                           \
        &at32f437_onchip_flash, \
        &nor_flash0,            \
    }
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
// 片内FLASH分区
#define BOOT_PARTITION (200 * 1024)     // 200KB
#define APP_PARTITION (3 * 1024 * 1024) // 3M
#define AP_PARTITION (1200 * 1024)      // 1200K
#define U200T_PARTITION (400 * 1024)    // 400K
#define ROOT_PARTITION (728 * 1024)     // 728KB
#define RESERVE_PARTITION (32 * 1024)   // 32kB
// 片内FLASH分区偏移
#define APP_OFFSET (BOOT_PARTITION)
#define ROOT_OFFSET (APP_OFFSET + APP_PARTITION)
#define RESERVE_OFFSET (ROOT_OFFSET + ROOT_PARTITION)

#define EXT_FAL_DOWN_NAME "down"
#define EXT_FAL_BACK_NAME "back"
#define EXT_FAL_AP_BIN_NAME "ap"
#define EXT_FAL_200T_BIN_NAME "200t"
#define EXT_FAL_LOGS_NAME "logs"
// 片外FLASH分区大小
#define EXT_FAL_DOWN_PARTITION (APP_PARTITION)
#define EXT_FAL_BACK_PARTITION (APP_PARTITION)
#define EXT_FAL_AP_PARTITION (AP_PARTITION)
#define EXT_FAL_200T_PARTITION (U200T_PARTITION)
// 外部flash的总大小 16 M
#define EXT_FAL_ALL_PARTITION (16 * 1024 * 1024)
#define EXT_FAL_LOGS_PARTITION (EXT_FAL_ALL_PARTITION - EXT_FAL_DOWN_PARTITION - EXT_FAL_BACK_PARTITION - EXT_FAL_AP_PARTITION - EXT_FAL_200T_PARTITION)
// 片外FLASH分区地址偏移
#define EXT_FAL_DOWN_OFFSET (0)
#define EXT_FAL_BACK_OFFSET (EXT_FAL_DOWN_OFFSET + EXT_FAL_DOWN_PARTITION)
#define EXT_FAL_AP_OFFSET (EXT_FAL_DOWN_OFFSET + EXT_FAL_BACK_PARTITION + EXT_FAL_BACK_OFFSET)
#define EXT_FAL_200T_OFFSET (EXT_FAL_DOWN_OFFSET + EXT_FAL_AP_PARTITION + EXT_FAL_AP_OFFSET)
#define EXT_FAL_LOGS_OFFSET (EXT_FAL_DOWN_OFFSET + EXT_FAL_200T_OFFSET + EXT_FAL_200T_PARTITION)

/* partition table */
#define FAL_PART_TABLE                                                                                                              \
    {                                                                                                                               \
        {FAL_PART_MAGIC_WORD, "boot", "at32_onchip", 0, BOOT_PARTITION, 0},                                                         \
        {FAL_PART_MAGIC_WORD, "app", "at32_onchip", APP_OFFSET, APP_PARTITION, 0},                                                  \
        {FAL_PART_MAGIC_WORD, "root", "at32_onchip", ROOT_OFFSET, ROOT_PARTITION, 0},                                               \
        {FAL_PART_MAGIC_WORD, "reserve", "at32_onchip", RESERVE_OFFSET, RESERVE_PARTITION, 0},                                      \
        {FAL_PART_MAGIC_WORD, EXT_FAL_DOWN_NAME, FAL_USING_NOR_FLASH_DEV_NAME, EXT_FAL_DOWN_OFFSET, EXT_FAL_DOWN_PARTITION, 0},     \
        {FAL_PART_MAGIC_WORD, EXT_FAL_BACK_NAME, FAL_USING_NOR_FLASH_DEV_NAME, EXT_FAL_BACK_OFFSET, EXT_FAL_BACK_PARTITION, 0},     \
        {FAL_PART_MAGIC_WORD, EXT_FAL_AP_BIN_NAME, FAL_USING_NOR_FLASH_DEV_NAME, EXT_FAL_AP_OFFSET, EXT_FAL_AP_PARTITION, 0},       \
        {FAL_PART_MAGIC_WORD, EXT_FAL_200T_BIN_NAME, FAL_USING_NOR_FLASH_DEV_NAME, EXT_FAL_200T_OFFSET, EXT_FAL_200T_PARTITION, 0}, \
        {FAL_PART_MAGIC_WORD, EXT_FAL_LOGS_NAME, FAL_USING_NOR_FLASH_DEV_NAME, EXT_FAL_LOGS_OFFSET, EXT_FAL_LOGS_PARTITION, 0},     \
    }
#endif

#endif /* _FAL_CFG_H_ */
